package com.epalmpay.service.management.impl;

import java.math.BigInteger;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.epalmpay.entity.Role;
import com.epalmpay.mapper.RoleMapper;
import com.epalmpay.service.management.IRoleService;
import com.epalmpay.util.RightsHelper;
import com.epalmpay.util.Tools;

@Service("roleService") 
public class RoleServiceImpl implements IRoleService {
	@Resource  
    private RoleMapper roleMapper;

	/**列出此组下级角色
	 * @param 
	 * @return
	 * @throws Exception
	 */
	public List<Role> listAllRolesByPId(Role role) throws Exception {
		return roleMapper.listAllRolesByPId(role);
	}
	
	/**通过id查找
	 * @param pd
	 * @return
	 * @throws Exception
	 */
	public Role findObjectById(Role role) throws Exception {
		return role; 
		
	}
	
	/**添加
	 * @param pd
	 * @throws Exception
	 */
	public void add(Role role) throws Exception {
		roleMapper.insert(role);
	}
	
	/**保存修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(Role role) throws Exception {
		roleMapper.updateByPrimaryKey(role);
	}
	
	/**删除角色
	 * @param ROLE_ID
	 * @throws Exception
	 */
	public void deleteRoleById(String roleId) throws Exception {
		roleMapper.deleteByPrimaryKey(roleId);
	}
	
	/**给当前角色附加菜单权限
	 * @param role
	 * @throws Exception
	 */
	public void updateRoleRights(Role role) throws Exception {
		
	}
	
	/**通过id查找
	 * @param roleId
	 * @return
	 * @throws Exception
	 */
	public Role getRoleById(String roleId) throws Exception {
		return roleMapper.selectByPrimaryKey(roleId);
	}
	
	/**给全部子角色加菜单权限
	 * @param pd
	 * @throws Exception
	 */
	public void setAllRights(Role pd) throws Exception {
		
	}
	
	/**权限(增删改查)
	 * @param msg 区分增删改查
	 * @param pd
	 * @throws Exception
	 */
	public void saveB4Button(String msg,Role pd) throws Exception {
		
	}

	@Override
	public Role findObjectByUserName(String userName) throws Exception {
		// TODO Auto-generated method stub
		return roleMapper.findObjectByUserName(userName); 
	}

	/**
	 * 
	 * 保存角色菜单权限
	 */
	@Override
	public void saveMenuqx(String roleId,String menuIds) throws Exception {
		Role role =null;
		if(null != menuIds && !"".equals(menuIds.trim())){
			BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));//用菜单ID做权处理
			role =roleMapper.selectByPrimaryKey(roleId);
			role.setRights(rights.toString()); 
			roleMapper.updateRoleRights(role);  //更新当前角色菜单权限
			role.setRights(rights.toString()); 
		}else{
		    role = new Role();
			role.setRights("");
			role.setRoleId(roleId);
			roleMapper.updateRoleRights(role); 			//更新当前角色菜单权限(没有任何勾选)
			role.setRights(""); 
		}
			role.setRoleId(roleId);
			roleMapper.setAllRights(role);
		
	}

	@Override
	public void saveB4Button(String roleId, String menuIds,String msg) throws Exception {
		Role role =new Role();
		role.setRoleId(roleId);
		if(null != menuIds && !"".equals(menuIds.trim())){
			BigInteger rights = RightsHelper.sumRights(Tools.str2StrArray(menuIds));
			if(msg.equals("add_qx")){
				role.setAddQx(rights.toString());
				roleMapper.addQx(role);
			}else if(msg.equals("del_qx")){
				role.setDelQx(rights.toString());
				roleMapper.deleteQx(role);
			}else if(msg.equals("edit_qx")){
				role.setEditQx(rights.toString());
				roleMapper.eidtQx(role);
			}else if(msg.equals("cha_qx")){
				role.setChaQx(rights.toString());
				roleMapper.selectQx(role);
			}
		}else{
			if(msg.equals("add_qx")){
				role.setAddQx("");
				roleMapper.addQx(role);
			}else if(msg.equals("del_qx")){
				role.setDelQx("");
				roleMapper.deleteQx(role);
			}else if(msg.equals("edit_qx")){
				role.setEditQx("");
				roleMapper.eidtQx(role);
			}else if(msg.equals("cha_qx")){
				role.setChaQx("");
				roleMapper.selectQx(role);
			}
		}
		
	}


	
	

	
}
